﻿package mortal.game.scene3D.display3d.text3d.staticText3d
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.display3D.*;
    import flash.events.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.render.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.registType.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.display3d.text3d.staticText3d.filter.*;

    public class SText3DRender extends DefaultRender
    {
        private var _textSurface:FrSurface3D;
        private var _textMaterial:ShaderBase;
        private var _textPosListRegister:VcParam;
        public var textBg0:Texture3D;
        public const maxNum:uint = 116;
        private static var _instance:SText3DRender;

        public function SText3DRender()
        {
            if (_instance && _instance != this)
            {
                return;
            }
            _instance = this;
            return;
        }// end function

        public function init(param1:GameScene3D) : void
        {
            this.initSurface();
            this.initMaterial(param1);
            return;
        }// end function

        public function reinit(param1:GameScene3D) : void
        {
            this.textBg0.reUpload();
            return;
        }// end function

        private function initMaterial(param1:GameScene3D) : void
        {
            this.textBg0 = new Texture3D("fightNumbers.png", 0);
            this.textBg0.loadedAndUpload = true;
            this._textMaterial = new ShaderBase("SText3DRender", new SText3DVertexFilter(), new SText3DFragmentFilter(this.textBg0));
            this._textMaterial.materialParams.setBlendMode(EBlendType.BLEND_ALPHA0);
            this._textMaterial.materialParams.depthWrite = false;
            this._textMaterial.materialParams.depthCompare = Context3DCompareMode.ALWAYS;
            FrEventDispatcher.instance.proxyAddEventListener(this._textMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            this._textMaterial.buildBytes();
            return;
        }// end function

        private function reBuildHander(event:Event) : void
        {
            this._textPosListRegister = this._textMaterial.getParam("{textPosList}", true);
            FrEventDispatcher.instance.proxyRemoveEventListener(this._textMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            return;
        }// end function

        private function initSurface() : void
        {
            var _loc_8:int = 0;
            this._textSurface = new FrSurface3D("texts");
            var _loc_1:* = this._textSurface.addVertexData(FilterName_ID.POSITION_ID, 3, true, null);
            this._textSurface.addVertexDataToTargetBuffer(FilterName_ID.UV_ID, 2, null, _loc_1);
            var _loc_2:* = this.createSinglePlaneVector(1, 1);
            var _loc_3:* = new Vector.<Number>;
            var _loc_4:* = this._textSurface.addVertexData(FilterName_ID.SKIN_INDICES_ID, 1, true, null);
            var _loc_5:* = _loc_4.vertexVector;
            var _loc_6:int = 0;
            while (_loc_6 < this.maxNum)
            {
                
                _loc_3 = _loc_3.concat(_loc_2);
                _loc_5.push(_loc_6, _loc_6, _loc_6, _loc_6);
                _loc_6++;
            }
            _loc_1.vertexVector = _loc_3;
            var _loc_7:* = new Vector.<uint>;
            _loc_6 = 0;
            while (_loc_6 < this.maxNum)
            {
                
                _loc_8 = _loc_6 * 4;
                _loc_7.push(_loc_8, _loc_8 + 2, (_loc_8 + 1));
                _loc_7.push(_loc_8 + 3, _loc_8 + 2, _loc_8);
                _loc_6++;
            }
            this._textSurface.indexVector = _loc_7;
            return;
        }// end function

        private function createSinglePlaneVector(param1:Number, param2:Number) : Vector.<Number>
        {
            var _loc_3:* = param2 * 0.5;
            var _loc_4:* = new Vector.<Number>;
            _loc_4.push(0, -_loc_3, 0, 0, 1);
            _loc_4.push(param1, -_loc_3, 0, 1, 1);
            _loc_4.push(param1, _loc_3, 0, 1, 0);
            _loc_4.push(0, _loc_3, 0, 0, 0);
            return _loc_4;
        }// end function

        override public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            var _loc_8:Vector.<Number> = null;
            if (!this._textPosListRegister)
            {
                return;
            }
            if (!this._textMaterial.hasPrepared(param1, this._textSurface))
            {
                return;
            }
            var _loc_3:* = SText3DMesh(param1);
            var _loc_4:* = _loc_3.vcListMap;
            var _loc_5:* = _loc_4.length;
            var _loc_6:* = this._textMaterial.materialParams;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_5)
            {
                
                var _loc_9:* = Global3D;
                var _loc_10:* = Global3D.objectsDrawn + 1;
                _loc_9.objectsDrawn = _loc_10;
                _loc_8 = _loc_4[_loc_7].list;
                this._textPosListRegister.value = _loc_8;
                this._textMaterial.draw(param1, this._textSurface, _loc_6.depthCompare, _loc_6.cullFace, false, _loc_6.sourceFactor, _loc_6.destFactor, 0, _loc_8.length * 0.5);
                _loc_7++;
            }
            return;
        }// end function

        public static function get instance() : SText3DRender
        {
            if (!_instance)
            {
                new SText3DRender;
            }
            return _instance;
        }// end function

    }
}
